/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JanelaExcluirJogo.java
 *
 * Created on Jun 28, 2011, 9:26:17 PM
 */
package funcenter.gui;

import funcenter.main.Main;
import java.io.File;
import javax.swing.JOptionPane;

/**
 *
 * @author arthur
 */
public class JanelaExcluirJogo extends javax.swing.JDialog {

    File[] pastas;
    
    /** Creates new form JanelaExcluirJogo */
    public JanelaExcluirJogo(java.awt.Frame parent, boolean modal, String[] listaJogos, File[] pastas) {
        super(parent, modal);
        initComponents();
        
        this.setLocationRelativeTo(parent);
        
        comboJogo.setModel(new javax.swing.DefaultComboBoxModel(listaJogos));
        this.pastas = pastas;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        comboJogo = new javax.swing.JComboBox();
        botaoRemover = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        getContentPane().setLayout(new java.awt.GridBagLayout());

        comboJogo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        comboJogo.setMinimumSize(new java.awt.Dimension(200, 24));
        comboJogo.setPreferredSize(new java.awt.Dimension(200, 24));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        getContentPane().add(comboJogo, gridBagConstraints);

        botaoRemover.setText("Remover");
        botaoRemover.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                clicarBotaoRemover(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        getContentPane().add(botaoRemover, gridBagConstraints);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void clicarBotaoRemover(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clicarBotaoRemover
        if(this.deleteDir(this.pastas[this.comboJogo.getSelectedIndex()])){
            JOptionPane.showMessageDialog(this, "Jogo excluído com sucesso!");
            Main.getJanelaPrincipal().carregarJogos();
        }        
    }//GEN-LAST:event_clicarBotaoRemover

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                JanelaExcluirJogo dialog = new JanelaExcluirJogo(new javax.swing.JFrame(), true, 
                        new String[0], new File[0]);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botaoRemover;
    private javax.swing.JComboBox comboJogo;
    // End of variables declaration//GEN-END:variables

    public boolean deleteDir(File dir) {
        if(dir.isDirectory()){
            String[] children = dir.list();
            for(int i = 0; i < children.length; i++){
                boolean success = deleteDir(new File(dir, children[i]));
                if(!success){
                    return false;
                }
            }
        }

        // Agora o diretório está vazio, restando apenas deletá-lo.
        return dir.delete();
    }
}
